﻿using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;
using MiniFox.Platform.File.Models;
using MiniFox.Platform.File.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Platform.Core.Providers
{
    [AutoResolve]
    public class GeneraicFileProvider : EntityDataProvider<PlatformDbContext>, IFileProvider
    {
        public FileData GetFile(string fileId)
        {
            var file = this.CurrentDbContext.File.SingleOrDefault(f => f.FileId == fileId);
            if (file == null) return null;
            return new ()
            {
                FileId = fileId,
                FileName = file.FileName,
                Path = file.Path,
                ContentType = file.MimeType,
                Group = file.Group,
                CreateTime = file.CreateTime,
                UpdateTime = file.UpdateTime
            };
        }

        public FileData GetFile(string group, string path, string fileName)
        {
            var file = this.CurrentDbContext.File.SingleOrDefault(f => f.Group == group && f.Path == path && f.FileName == fileName);
            if (file == null) return null;
            return new()
            {
                FileId = file.FileId,
                FileName = file.FileName,
                Path = file.Path,
                ContentType = file.MimeType,
                Group = file.Group,
                CreateTime = file.CreateTime,
                UpdateTime = file.UpdateTime
            };
        }

        public ResultObject<IEnumerable<FileData>> Query(string group, PagedQueryObject<FileQuery> queryObject)
        {
            var fileTypes = queryObject.Parameters.FileTypes;
            var query = (from f in this.CurrentDbContext.File where f.Group == @group && (fileTypes == null || fileTypes.Length <= 0 || fileTypes.Contains(f.ExtName)) select f);
            var result = this.PagedQueryExecute(query, queryObject);
            return result.GetResultObject(f => new FileData
            {
                FileId = f.FileId,
                FileName = f.FileName,
                Path = f.Path,
                ContentType = f.MimeType,
                Group = f.Group,
                CreateTime = f.CreateTime,
                UpdateTime = f.UpdateTime
            });
        }

        public FileData[] Query(string group, string path, int? top)
        {
            var fileDatas = (from f in this.CurrentDbContext.File
                             where f.Group == @group && (f.Path == path || f.Path.StartsWith($"{path}/"))
                             select new FileData
                             {
                                 FileId = f.FileId,
                                 FileName = f.FileName,
                                 Path = f.Path,
                                 ContentType = f.MimeType,
                                 Group = f.Group,
                                 CreateTime = f.CreateTime,
                                 UpdateTime = f.UpdateTime
                             });
            return fileDatas.ToArray();
        }

        //public FileData[] Query(string folderId)
        //{
        //    var folder = this.CurrentDbContext.Folder.SingleOrDefault(f => f.FolderId == folderId);
        //    var fileDatas = (from f in this.CurrentDbContext.File
        //                     where f.Group == folder.Group && f.Path == folder.Path
        //                     select new FileData
        //                     {
        //                         FileId = f.FileId,
        //                         FileName = f.FileName,
        //                         Path = f.Path,
        //                         ContentType = f.MimeType,
        //                         Group = f.Group,
        //                         CreateTime = f.CreateTime,
        //                         UpdateTime = f.UpdateTime
        //                     });
        //    return fileDatas.ToArray();
        //}
    }
}
